package com.maomi.coder;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;

/**
 * 描述:
 * 从文件中导入索引数据
 *
 * @author luzhongfu
 * @create 2019-03-25 10:46 PM
 */
public class FileIndexer {

    private IndexWriter writer;

    public void getWriter(String url) throws IOException {
        //1.创建文件目录
        Directory dir = FSDirectory.open(Paths.get(url));
        //2.创建分词器
        Analyzer analyzer = new StandardAnalyzer();
        //3.创建写索引配置
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        //4.创建写索引
        writer = new IndexWriter(dir,indexWriterConfig);
    }

    public int index(String url) throws IOException {
        File file = new File(url);
        File[] files = file.listFiles();
        for (File f:files) {
            indexFile(f);
        }
        return writer.numDocs();
    }

    public void indexFile(File f) throws IOException {
        Document doc = getDocument(f);
        writer.addDocument(doc);
    }

    public Document getDocument(File f) throws FileNotFoundException {
        Document d = new Document();
        //以流文件形式存储
        d.add(new TextField("txtContent",new FileReader(f)));
        d.add(new TextField("fileName",f.getName(), Field.Store.YES));
        d.add(new TextField("url",f.getAbsolutePath(),Field.Store.YES));
        return d;
    }

    public void close() throws IOException {
        if(writer != null){
            writer.close();
        }
    }

    public void read() throws IOException, ParseException {
        Directory directory = FSDirectory.open(Paths.get("/Users/luzhongfu/Downloads/lucene/index2"));
        DirectoryReader open = DirectoryReader.open(directory);

        IndexSearcher is = new IndexSearcher(open);
        Analyzer analyzer = new StandardAnalyzer();

        QueryParser query = new QueryParser("txtContent", analyzer);
        Query parse = query.parse("hello");
        TopDocs search = is.search(parse, 10);

        for (ScoreDoc doc:search.scoreDocs) {
            Document doc1 = is.doc(doc.doc);
            System.out.println(doc1.get("fileName"));
            System.out.println(doc1.get("url"));
        }
        open.close();

    }

    public static void main(String[] args) throws IOException, ParseException {
        String dataDir = "/Users/luzhongfu/Downloads/lucene/data";
        String indexDir = "/Users/luzhongfu/Downloads/lucene/index2";
        FileIndexer indexer = new FileIndexer();
        //创建 indexWrite
//        indexer.getWriter(indexDir);
//        int num = indexer.index(dataDir);
//        System.out.println(num);
//
//        indexer.close();
        indexer.read();

    }
}
